#include "check_login.h"
#include "Upload_database.h"
#include "list.h"
#include "sys_log.h"

// 检查管理员登录
int check_login1(USE *data)
{
	int flag = 0;
	MYSQL *ckconnect = NULL;
	// 链接数据库
	ckconnect = connect_mysql();

	USE *head = NULL;
	head = List_init();
	// 从数据库读取管理员信息到链表
	head = read_mysql_to_list(ckconnect, head);
	printf("从MySQL中拿到的管理员链表的数据：\n");
	Printf_list(head);

	// 比较链表与输入的账号密码
	flag = while_check_login(head, data);

	return flag;
	freeLink(head);
	mysql_close(ckconnect);
}

// 遍历链表检查输入是否正确
int while_check_login(USE *head, USE *data)
{
	int flag = 0;
	USE *p = head;
	while (p != NULL)
	{
		if (strcmp(p->user_name, data->user_name) == 0 && strcmp(p->user_password, data->user_password) == 0)
		{
			printf("管理员登录成功！\n");
			flag = 1;
			return flag;
		}
		p = p->Next;
	}
	if (flag == 0)
	{
		printf("账号或密码错误,管理员登录失败！\n");
		return flag;
	}
	return flag;
}

// 检查用户登录
int check_login2(USE *data)
{
	int flag = 0;
	MYSQL *ckconnect = NULL;
	ckconnect = connect_mysql();

	USE *head = NULL;
	head = List_init();
	// 从数据库读取用户信息到链表
	head = read_mysql_to_list2(ckconnect, head);
	printf("从MySQL中拿到的用户链表的数据：\n");
	Printf_list(head);

	// 比较链表与输入的账号密码
	flag = while_check_login(head, data);

	return flag;
	freeLink(head);
	mysql_close(ckconnect);
}

// 遍历用户链表检查输入是否正确
int while_check_login2(USE *head, USE *data)
{
	int flag = 0;
	USE *p = head;
	while (p != NULL)
	{
		if (strcmp(p->user_name, data->user_name) == 0 && strcmp(p->user_password, data->user_password) == 0)
		{
			printf("用户登录成功！\n");
			flag = 1;
			return flag;
		}
		p = p->Next;
	}
	if (flag == 0)
	{
		printf("账号或密码错误,用户登录失败！\n");
		return flag;
	}
	return flag;
}
